package org.halk.config;

import org.halk.aspect.LockMemoryServiceImpl;
import org.halk.aspect.LockRedisServiceImpl;
import org.halk.aspect.LockService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * 配置bena，使用配置文件配置哪个bean注入容器
 * @Author haozhu7
 * @Date 2024/9/19 16:49
 **/
@Configuration
public class AppConfig {

    @Value("${halk.lock.type:redis}")
    private String lockType;

    @Bean
    public LockRedisServiceImpl redisService() {
        return new LockRedisServiceImpl();
    }

    @Bean
    public LockMemoryServiceImpl memoryService() {
        return new LockMemoryServiceImpl();
    }

    @Bean
    @Primary
    public LockService myService() {
        if ("redis".equals(lockType)) {
            return redisService();
        } else if ("memory".equals(lockType)) {
            return memoryService();
        } else {
            throw new IllegalArgumentException("Invalid service implementation: " + lockType);
        }
    }
}
